package main.gui;

import java.awt.Cursor;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import main.model.Settings;

@SuppressWarnings("serial")
public class NarwhalFrame extends JFrame {
	private GraphicsDevice vc;
	private DisplayMode screenStandard;
	private DisplayMode narwhalStandard;

	public NarwhalFrame() {
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		vc = ge.getDefaultScreenDevice();
		screenStandard = vc.getDisplayMode();
		narwhalStandard = new DisplayMode(1024, 768,
				DisplayMode.BIT_DEPTH_MULTI, DisplayMode.REFRESH_RATE_UNKNOWN);

		if (!new Settings().showMouse()) {
			BufferedImage cursorImg = new BufferedImage(16, 16,
					BufferedImage.TYPE_INT_ARGB);
			Cursor blankCursor = Toolkit.getDefaultToolkit()
					.createCustomCursor(cursorImg, new Point(0, 0),
							"blank cursor");
			setCursor(blankCursor);
		}

		setFullScreen();
		addFocusListener(new Focus());
		setUndecorated(true);
		setResizable(false);
		vc.setFullScreenWindow(this);
	}

	public void setFullScreen() {
		System.out.println("Enable fullscreen (" + System.currentTimeMillis()
				+ ")");
		if (vc.isDisplayChangeSupported()) {
			vc.setDisplayMode(narwhalStandard);
		}
	}

	public void restoreScreen() {
		System.out.println("Disable fullscreen (" + System.currentTimeMillis()
				+ ")");
		if (vc.isDisplayChangeSupported()) {
			vc.setDisplayMode(screenStandard);
		}
	}

	private class Focus implements FocusListener {

		@Override
		public void focusGained(FocusEvent arg0) {
			setFullScreen();
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			restoreScreen();
		}
	}
}
